package com.zuihai.app.mvp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.jess.arms.utils.KnifeUtil;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.widget.MyToast;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/6/6 0006.
 */

public class CustomMoneyDialog extends Dialog {

    private Build mBuild;

    @BindView(R.id.iv_logo)
    CircleImageView mLogoView;

    @BindView(R.id.et_money_input)
    EditText mInputView;
    private double mCustomMoney;

    public CustomMoneyDialog(Build build) {
        this(build.mContext, R.style.centerDialog);
        mBuild = build;
    }

    private CustomMoneyDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom_price);
        KnifeUtil.bindTarget(this, this);
        initWindow();
        init();
    }

    private void init(){
        if (mBuild.mLogo != 0){
            mLogoView.setVisibility(View.VISIBLE);
            mLogoView.setImageResource(mBuild.mLogo);
        }
        //mInputView.setFilters(new InputFilter[]{new MoneyInputFilter()});
        RxTextView.textChanges(mInputView)
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        mCustomMoney = Double.valueOf(TextUtils.isEmpty(charSequence) ? "0.0" : charSequence.toString());
                    }
                });
    }

    /**
     * 初始化窗口信息
     */
    private void initWindow() {
        Window window = getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.CENTER);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为包裹内容
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //设置dialog点击外部不消失
        setCanceledOnTouchOutside(false);
    }

    /**
     * 获取输入的金额
     * @return
     */
    public double getMoney(){
        return mCustomMoney;
    }

    /**
     * 取消
     * @param view
     */
    @OnClick(R.id.tv_cancle)
    public void onCancle(View view){
        dismiss();
        if (mBuild.mCancleListener != null){
            mBuild.mCancleListener.onClick(view);
        }
    }

    @OnClick(R.id.tv_confirm)
    public void onConfirm(View view){
        if (mCustomMoney < 0.01) {
            MyToast.showError("输入的金额不能少于0.1元，请重新填写");
            return;
        }
        if (mBuild.mConfirmListener != null){
            mBuild.mConfirmListener.onClick(this, view);
        }
    }

    public static class Build{

        private View.OnClickListener mCancleListener;
        private OnConfirmListener mConfirmListener;
        private Context mContext;
        private int mLogo;

        public Build(Context context) {
            mContext = context;
        }

        public CustomMoneyDialog create(){
            CustomMoneyDialog dialog = new CustomMoneyDialog(this);
            return dialog;
        }

        public Build setConfirmListener(OnConfirmListener mConfirmListener) {
            this.mConfirmListener = mConfirmListener;
            return this;
        }

        public Build setCancleListener(View.OnClickListener mCancleListener) {
            this.mCancleListener = mCancleListener;
            return this;
        }

        public Build setLogoResuorce(@DrawableRes int drawable){
            this.mLogo = drawable;
            return this;
        }
    }

    public interface OnConfirmListener{
        void onClick(CustomMoneyDialog dialog, View v);
    }
}
